feat(testing): add N+1 query detection helper and fix list endpoints (#1243) - #1423
Open
xtep103 wants to merge 1 commit into
Open
Conversation
- Introduce AsyncLocalStorage query counter and Prisma middleware (countQueries, assertConstantQueryCount) - Fix N+1 queries in profiles.service.ts (batch tip stats aggregation in listProfiles) - Fix N+1 queries in analytics.service.ts (batch user lookups in getTopTippers & getCreatorAnalytics) - Add comprehensive unit and endpoint N+1 test suites - Add N+1 testing documentation and update contributing guide Closes Akanimoh12#1243
|
@xtep103 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Owner
|
Merge conflicts with test-implement-drips — please rebase and resolve so this can be merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Implements automated N+1 query detection and regression testing helpers for Prisma, and resolves existing N+1 query patterns across
profilesandanalyticsservices.Related Issue
Closes #1243
Changes
Testing Framework & Database Observability
backend/src/common/testing/queryCounter.tsqueryCounterMiddlewareusing Node'sAsyncLocalStorageto track Prisma queries per async context.countQueries(action)helper to inspect exact queries and execution count.assertConstantQueryCount(runner, sizes)helper to verify that query count remains constant regardless of result-set / page size.backend/src/common/testing/queryCounter.test.tsbackend/tests/helpers/queryCounter.tsbackend/tests/nPlusOne.test.tsbackend/src/db/prisma.tsqueryCounterMiddlewareon the singleton Prisma client (no-op in production, active only within test counting contexts).Service Optimization & N+1 Fixes
backend/src/modules/profiles/profiles.service.tsgetTipStats()calls inlistProfileswith a single batchedprisma.tip.groupBy({ where: { toAddress: { in: addresses } } })query, reducing queries from O(2N + 1) to O(3) constant.backend/src/modules/analytics/analytics.service.tsprisma.user.findUniquelookups ingetTopTipperswith a single batchedprisma.user.findMany({ where: { stellarAddress: { in: addresses } } }).prisma.user.findUniquelookups for top tippers ingetCreatorAnalyticswith a single batchedprisma.user.findMany.Documentation
backend/docs/N_PLUS_ONE_DETECTION.mdbackend/docs/BACKEND_CONTRIBUTING.mdVerification Results
AsyncLocalStorage& Prisma middlewarecountQueries,assertConstantQueryCount)profilesandanalyticsgroupByandfindManyinqueriesdocs/N_PLUS_ONE_DETECTION.mdadded and contributing guide updated